package top.hkyzf.study.leetcode.tree;

import java.util.List;

/**
 * @author 朱峰
 * @date 2022-1-26 10:38
 */
public class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode next;
    List<TreeNode> children;

    TreeNode() {

    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }

    TreeNode(int val, TreeNode left, TreeNode right, TreeNode next) {
        this.val = val;
        this.left = left;
        this.right = right;
        this.next = next;
    }

    public void print() {
        dfs(this);
    }

    private void dfs(TreeNode node) {
        if(node != null) {
             System.out.println(node);
             dfs(node.left);
             dfs(node.right);
        }
    }

    public void printNext() {
        dfsNext(this);
    }

    private void dfsNext(TreeNode node) {
        if (node != null) {
            printNext(node);
            dfsNext(node.left);
        }
    }

    private void printNext(TreeNode node) {
        if (node != null) {
            System.out.println(node);
            if (node.next != null) {
                printNext(node.next);
            }
        }
    }

    @Override
    public String toString() {
        return "TreeNode{" +
                "val=" + val +
                ", left=" + (left==null ? "null" : left.val) +
                ", right=" + (right==null ? "null" : right.val) +
                '}';
    }
}
